<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.backupsName" placeholder="请输入备份名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenBackups()" v-hasPermi="['admin:backups:save']">立即备份</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="备份名称" prop="backupsName" min-width="120"/>
        <el-table-column label="备份时间" prop="createTimeText" min-width="120"/>
        <el-table-column label="数据大小" prop="sizeText" min-width="120"/>
        <el-table-column label="操作" min-width="130"  fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-refresh-right" type="success" size="mini" @click="handlerOpenRestore(scope.row)" v-hasPermi="['admin:backups:restore']">还原</el-button>
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenDown(scope.row)" v-hasPermi="['admin:backups:down']">下载</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:backups:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import * as backupsApi from '@/api/backups.js'
import * as roleApi from '@/api/role.js'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil";
import {createFile} from "@/utils/utils"; // 权限判断函数
export default {
  // name: "index"
  components: { },
  data() {
    return {
      constants:this.$constants,
      loading: false,
      listData: { list: [] },
      listPram: {
        backupsName: '',
        /*departmentId: null,
        ipAddress: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },
    }
  },
  mounted() {
    this.handleGetBackupsList()
  },
  methods: {
    checkPermi,
    handleSearch() {
      this.listPram.page = 1
      this.handleGetBackupsList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetBackupsList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetBackupsList()
    },
    handlerOpenBackups() {
      this.loading = true

      this.$confirm('确认备份当前数据').then(() => {
        backupsApi.backupsDo({}).then(data => {
          this.$message.success('备份数据成功')
          this.handleGetBackupsList()
          this.loading = false
        })
      })
    },
    handlerOpenRestore(rowData) {
      this.loading = true

      this.$confirm('确认还原当前数据').then(() => {
        const _pram = { id: rowData.id}
        backupsApi.backupsRestore(_pram).then(data => {
          this.$message.success('还原数据成功')
          this.handleGetBackupsList()
          this.loading = false
        })
      })
    },
    handlerOpenDown(rowData) {
      this.$confirm('确认下载当前数据').then(() => {
        const _pram = { id: rowData.id}
        let loading = this.$loading({
          lock: true,
          text: "导出中，请稍候...",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        backupsApi.backupsDown(_pram)
          .then((res) => {

            if(!createFile(res,'数据文件备份')){
              this.$message.success("未知的文件格式");
            }

            loading.close();
          })
          .catch((res) => {
            loading.close();
          });
      })
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        backupsApi.backupsDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetBackupsList()
        })
      })
    },

    handleGetBackupsList() {
      backupsApi.backupsList( this.listPram ).then(data => {
        this.listData = data
      })
    },
    resetQuery(){
      this.listPram.backupsName = '';
      this.handleGetBackupsList()
    }
  }
}
</script>

<style scoped>

</style>
